<?php



class Backend_TranslateController extends RCMS_Core_BaseController {

	protected $_model;

	protected $_configArray;


	public function init() {
		parent::init();
		parent::checkLogin();
		$this->_model = new Default_Model_TranslateModel();
		$this->_configArray = $this->_model->selectAllConfigData();
		$this->_cache->clean('all');
	}

	public function indexAction() {
		$this->_helper->getHelper('layout')->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
	}


	public function translateAction() {
		//header("Content-Type: text/html; charset=UTF-8");
		$this->_helper->getHelper('layout')->disableLayout();
		$form = new RCMS_Form_TranslateForm();
		$form->setDecorators(array(
			array('ViewScript', array('viewScript' => 'forms/translate.phtml'))
		));
		$lexemId = $this->getRequest()->getParam('lexemId');
		$lexemObj = $this->_model->selectLexemById($lexemId);
		$languageId = $this->_session->translatedLanguageId;
		$languageName = $this->_model->selectLanguageById($languageId);

		$this->view->form = $form;

		if($this->getRequest()->isPost()) {
			if(trim($this->getRequest()->getParam('lexemtext'))!="") {
				$translatedText = trim($this->getRequest()->getParam('lexemtext'));
				$this->_model->insertTranslatedValue($lexemId, $languageId, $translatedText);
				echo '<script type="text/javascript">parent.tb_remove();parent.location.reload();</script>';
			}else {
				$this->view->error = true;
			}
		}
		$this->view->screenTitle = 'Translate lexem "'.$lexemObj->original_value.'" in '.$languageName.' language.';
	}

	public function editAction() {
		//header("Content-Type: text/html; charset=UTF-8");
		$this->_helper->getHelper('layout')->disableLayout();
		$form = new RCMS_Form_TranslateForm();
		$form->setDecorators(array(
			array('ViewScript', array('viewScript' => 'forms/translate.phtml'))
		));
		$lexemId = $this->getRequest()->getParam('lexemId');
		$languageId = $this->_session->translatedLanguageId;
		$lexemObj = $this->_model->selectLexemById($lexemId);
		$translatedObj = $this->_model->selectTranslatedLexem($lexemId, $languageId);
		$form->getElement("lexemtext")->setValue($translatedObj->value);
		$languageId = $this->_session->translatedLanguageId;
		$languageName = $this->_model->selectLanguageById($languageId);
		$this->view->form = $form;
		if($this->getRequest()->isPost()) {
			if(trim($this->getRequest()->getParam('lexemtext'))!="") {
				$translatedText = $this->getRequest()->getParam('lexemtext');
				$this->_model->updateTranslatedValue($lexemId, $languageId, $translatedText);
				echo '<script type="text/javascript">parent.tb_remove();parent.location.reload();</script>';
			}
			else {
				$this->view->error = true;
			}
		}
		$this->view->screenTitle = 'Edit lexem "'.$lexemObj->original_value.'" in '.$languageName.' language.';

	}

public function manageAction() {
		$this->_helper->getHelper('layout')->disableLayout();
		$form = new RCMS_Form_LanguagesForm();
		$form->setDecorators(array(
			array('ViewScript', array('viewScript' => 'forms/managelanguage.phtml'))
		));
		$this->view->form = $form;


		if($this->getRequest()->isPost()) {
			if(trim($this->getRequest()->getParam('lexemtext'))!="") {
				$translatedText = trim($this->getRequest()->getParam('lexemtext'));
				$this->_model->insertTranslatedValue($lexemId, $languageId, $translatedText);
				echo '<script type="text/javascript">parent.tb_remove();parent.location.reload();</script>';
			}else {
				$this->view->error = true;
			}
		}
	}

	/**
	 * This function generates tomorrows date
	 * @return <date>
	 */
	public function generateTomorrowsDate() {
		$date = date('m-d-Y');
		$dateArray = explode("-", $date);
		$tomorrowsDate = date("Y-m-d", mktime(0, 0, 0, $dateArray[0], ($dateArray[1])+1,$dateArray[2]));
		return $tomorrowsDate;
	}
	/**
	 * This function generates todays date
	 * @return <date>
	 */
	public function generateTodaysDate() {
		return date('Y-m-d');
	}
	/**
	 * This function generates date to sql format
	 * @return <date>
	 */
	public function proccessPublishingDateToSql($date) {
		$dateArray = explode("-", trim($date));
		$date = date("Y-m-d", mktime(0, 0, 0, $dateArray[0], $dateArray[1],$dateArray[2]));
		return $date;
	}
	/**
	 * This function generates date from sql format
	 * @return <date>
	 */
	public function proccessPublishingDateFromSql($date) {
		$dateArray = explode("-", trim($date));

		$timeArray = explode(" ", $dateArray[2]);

		$day = $timeArray[0];
		$date = date("m-d-Y", mktime(0, 0, 0, $dateArray[1], $day,$dateArray[0]));
		return $date;
	}

	public function selectLexemById($lexemId) {

	}
}